package org.jeecg.modules.flower.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.flower.entity.FlowerLose;
import org.jeecg.modules.flower.entity.FlowerTagpre;
import org.jeecg.modules.flower.exception.FlowerLoseNumException;
import org.jeecg.modules.flower.service.IFlowerLoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 蔬菜货损
 * @Author: jeecg-boot
 * @Date:   2022-06-29
 * @Version: V1.0
 */
@Api(tags="蔬菜货损")
@RestController
@RequestMapping("/flower/flowerLose")
@Slf4j
public class FlowerLoseController extends JeecgController<FlowerLose, IFlowerLoseService> {
	@Autowired
	private IFlowerLoseService flowerLoseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param flowerLose
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-分页列表查询")
	@ApiOperation(value="蔬菜货损-分页列表查询", notes="蔬菜货损-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FlowerLose flowerLose,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FlowerLose> queryWrapper = QueryGenerator.initQueryWrapper(flowerLose, req.getParameterMap());
		Page<FlowerLose> page = new Page<>(pageNo, pageSize);

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
		queryWrapper.inSql("storehouse","select b.code from flower_storehouse b , flower_storehouse_detail c where b.id = c.mainid   and c.personid = '"+sysUser.getId()+"'");
//		queryWrapper.exists("select 1 from flower_storehouse b , flower_storehouse_detail c where b.id = c.mainid and b.code=storehouse  and c.personid = '"+sysUser.getId()+"'");

		IPage<FlowerLose> pageList = flowerLoseService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param flowerLose
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-添加")
	@ApiOperation(value="蔬菜货损-添加", notes="蔬菜货损-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FlowerLose flowerLose) {

		try {
			flowerLoseService.customSave(flowerLose);
		} catch (Exception e) {
			if(e instanceof FlowerLoseNumException){
				throw new FlowerLoseNumException(e);
			}
			e.printStackTrace();
			return Result.ok("添加失败！");
		}

		return Result.ok("添加成功！");
	}

	@ResponseBody
	@ApiOperation(value="蔬菜货损-确认", notes="蔬菜货损-确认")
	@PostMapping("/flowerLose")
	public Result<?> flowerLose(@RequestBody FlowerLose flowerLose) {
		try {
			flowerLoseService.loseData(flowerLose);
		} catch (Exception e) {
			if(e instanceof FlowerLoseNumException){
				throw new FlowerLoseNumException(e);
			}
			e.printStackTrace();
			return Result.ok("库存扣减失败！");
		}

		return Result.ok("库存扣减成功！");
	}

	/**
	 *  编辑
	 *
	 * @param flowerLose
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-编辑")
	@ApiOperation(value="蔬菜货损-编辑", notes="蔬菜货损-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FlowerLose flowerLose) {
		try {
			flowerLoseService.customEdit(flowerLose);
		} catch (Exception e) {
			if(e instanceof FlowerLoseNumException){
				throw new FlowerLoseNumException(e);
			}
			e.printStackTrace();
			return Result.ok("编辑失败！");
		}

		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-通过id删除")
	@ApiOperation(value="蔬菜货损-通过id删除", notes="蔬菜货损-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		flowerLoseService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-批量删除")
	@ApiOperation(value="蔬菜货损-批量删除", notes="蔬菜货损-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.flowerLoseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "蔬菜货损-通过id查询")
	@ApiOperation(value="蔬菜货损-通过id查询", notes="蔬菜货损-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FlowerLose flowerLose = flowerLoseService.getById(id);
		if(flowerLose==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(flowerLose);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param flowerLose
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FlowerLose flowerLose) {
        return super.exportXls(request, flowerLose, FlowerLose.class, "蔬菜货损");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FlowerLose.class);
    }

}
